package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.PChatDetailsBo;
import com.ruoyi.system.domain.vo.PChatDetailsVo;
import com.ruoyi.system.domain.PChatDetails;
import com.ruoyi.system.mapper.PChatDetailsMapper;
import com.ruoyi.system.service.IPChatDetailsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 聊天详情Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-22
 */
@RequiredArgsConstructor
@Service
public class PChatDetailsServiceImpl implements IPChatDetailsService {

    private final PChatDetailsMapper baseMapper;

    /**
     * 查询聊天详情
     */
    @Override
    public PChatDetailsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询聊天详情列表
     */
    @Override
    public TableDataInfo<PChatDetailsVo> queryPageList(PChatDetailsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PChatDetails> lqw = buildQueryWrapper(bo);
        Page<PChatDetailsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询聊天详情列表
     */
    @Override
    public List<PChatDetailsVo> queryList(PChatDetailsBo bo) {
        LambdaQueryWrapper<PChatDetails> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PChatDetails> buildQueryWrapper(PChatDetailsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PChatDetails> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getChatId() != null, PChatDetails::getChatId, bo.getChatId());
        lqw.eq(bo.getUserId() != null, PChatDetails::getUserId, bo.getUserId());
        lqw.eq(bo.getOrderId() != null, PChatDetails::getOrderId, bo.getOrderId());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), PChatDetails::getContent, bo.getContent());
        lqw.eq(bo.getIsLatest() != null, PChatDetails::getIsLatest, bo.getIsLatest());
        return lqw;
    }

    /**
     * 新增聊天详情
     */
    @Override
    public Boolean insertByBo(PChatDetailsBo bo) {
        PChatDetails add = BeanUtil.toBean(bo, PChatDetails.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改聊天详情
     */
    @Override
    public Boolean updateByBo(PChatDetailsBo bo) {
        PChatDetails update = BeanUtil.toBean(bo, PChatDetails.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PChatDetails entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除聊天详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        baseMapper.deleteBatchIds(ids);
        return (Boolean) true;
    }

    @Override
    public PChatDetails cancelByParams(Long userId,Long chatId,String orderId) {
        return baseMapper.cancelByParams(userId,chatId,orderId);
    }

    @Override
    public List<PChatDetailsBo> pChatDetailsList(Long chatId,int page) {
        return baseMapper.pChatDetailsList(chatId,page);
    }

    @Override
    public List<PChatDetailsBo> pChatDetailsLists(Long chatId,int page, String order) {
        return baseMapper.pChatDetailsLists(chatId,page,order);
    }
}
